<?php
namespace Home\Controller;

use Home\Model\OfficeStationeryModel;
use Home\Model\ApplicationFormModel;

class OfficeStationeryController extends CommonController{
	
  protected $table="office_stationery";
  protected $pageSize = 13;
  
  public function index(){
	$type = D('OfficeStationery')->where(array('info_status'=>1))->select();
	$this->assign('type', $this->M->getTypes());
	$this->assign('attributes', $this->M->getAttributes());
	$this->assign('belongsDepartment',D('ApplicationForm')->getSelfDepartments());
	$this->assign('departName',D('ApplicationForm')->getSelfDepartment());
  	$this->display();
  }


  public function getList(){
	$this->setQeuryCondition();
    $count = (int)$this->M->count();
    $this->setQeuryCondition();
    $list = $this->M->page(I('get.p',0). ','. $this->pageSize)->order('id desc')->select();
    $result = array('list'=>$list, 'count'=>$count);
		$this->ajaxReturn($result);
  }

 	//设置查询
  public function setQeuryCondition(){
  	if (isset($_GET['belongs_department'])) {
		$this->M->where(array("belongs_department"=> array('EQ', I('get.belongs_department'))));
	}else{
		$this->M->where(array("belongs_department"=> array('EQ', D('ApplicationForm')->getSelfDepartment())));
	}

	if (I('get.type')) {
		$this->M->where(array("type"=> array('EQ', I('get.type'))));
	}
	
	if (I('get.name')) {
		$this->M->where(array("name"=> array('like', "%".I('get.name')."%")));
	}
	
    $this->M->where( array('info_status'=>array('EQ', 1) ) );
  }
  
  //获取用品资料信息
  public function getData(){
    $list = D('OfficeStationery')->distinct(true)->where(array('info_status'=>1,'status'=>1,'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->getField('name',true);
    $this->ajaxReturn($list);
  }

  public function getSpecByName($name){
    $list = D('OfficeStationery')->distinct(true)->where(array('info_status'=>1,'status'=>1,'name'=>$name,'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->getField('specification',true);
    $this->ajaxReturn($list);
  }
  public function getUnitsBySpec($specification,$name){
    $list = D('OfficeStationery')->distinct(true)->where(array('info_status'=>1,'status'=>1, 'specification'=>$specification, 'name'=>$name,'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->getField('unit',true);
    $this->ajaxReturn($list);
  }

  
    public function add() {
      $_POST['operator'] = $_SESSION['account']['userInfo']['user_id'];
	  $_POST['belongs_department'] = D('ApplicationForm')->getSelfDepartment();
	  $res = D('OfficeStationery')->where(array('info_status'=>1,'name'=>$_POST['name'],'specification'=>$_POST['specification'],'unit'=>$_POST['unit'],'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->select();
	  if($res){
	  	$this->error('该用品资料已存在，请不要重复录入！');
	  }else{
	  	if (!empty($_POST) && $this->M->create($_POST, 1) && $this->M->add()) {
		  $this->success(L('ADD_SUCCESS'));
	    } else {
		  $this->error($this->M->getError());
	    }
	  }
	}

    public function delete() {
		$id = I("post.ids");
		$id = $id[0];
		$re = D('OfficeStationery')->where(array('info_status'=>1,'id'=>$id,'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->find();
		$res = D('StorageDetail')->where(array('name'=>$re['name'],'specification'=>$re['specification'],'unit'=>$re['unit'],'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->select();
		if($res){
			$this->error('该用品资料已在使用中，请不要删除！');
		}else{
			if($this->M->data(array('info_status'=>-1))->where(array('id'=>array('in', $id)))->save()){
				$this->success('删除成功！');
			}
			
		}
	}



















}